package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.base.CommonTree;
import com.uwlaser.domain.ProductModel;
import com.uwlaser.service.ProductModelService;
import com.uwlaser.service.dto.ProductModelDto;
import com.uwlaser.service.dto.ProductModelQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "产品型号管理")
@RequestMapping("/api/productModel")
public class ProductModelController {

    private final ProductModelService productModelService;

    @GetMapping
    @Log("查询产品型号")
    @ApiOperation("查询产品型号")
    @PreAuthorize("@el.check('productModel:list')")
    public ResponseEntity query(ProductModelQueryParam query, Pageable pageable){
        return new ResponseEntity<>(productModelService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询产品型号-列表")
    @ApiOperation("查询产品型号-列表")
    @PreAuthorize("@el.check('productModel:list','dataMaterialBatch:list')")
    public ResponseEntity list(ProductModelQueryParam query){
        return new ResponseEntity<>(productModelService.queryAll(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增产品型号")
    @ApiOperation("新增产品型号")
    @PreAuthorize("@el.check('productModel:add')")
    public ResponseEntity create(@Validated @RequestBody ProductModelDto resources){
        return new ResponseEntity<>(productModelService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改产品型号")
    @ApiOperation("修改产品型号")
    @PreAuthorize("@el.check('productModel:edit')")
    public ResponseEntity update(@Validated @RequestBody ProductModelDto resources){
        productModelService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除产品型号")
    @ApiOperation("删除产品型号")
    @PreAuthorize("@el.check('productModel:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        productModelService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/getTree")
    @Log("获取产品型号树结构")
    @ApiOperation("获取产品型号树结构")
    @PreAuthorize("@el.check('productModel:list')")
    public ResponseEntity getTree() {
        List<CommonTree> list = productModelService.getTree();
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    /*
    @Log("导出产品型号")
    @ApiOperation("导出产品型号")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('productModel:list')")
    public void download(HttpServletResponse response, ProductModelQueryParam query) throws IOException {
        productModelService.download(productModelService.queryAll(query), response);
    }*/

}
